package com.cory.module.msg.admin.aspect;

import java.lang.reflect.Method;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import com.cory.module.msg.admin.AdminMsg;
import com.cory.module.msg.admin.AdminMsgClient;
import com.cory.module.msg.admin.annotation.AdminMsgReload;

/**
 * @author xiongcong
 * @date 2022-09-17
 */
@Aspect
@Component
public class AdminMsgAspect {

    @Pointcut("@annotation(com.cory.module.msg.admin.annotation.AdminMsgReload)")
    public void adminMsgReloadAspect() {}
    
    @AfterReturning("adminMsgReloadAspect()")
    public void adminMsgReload(JoinPoint pjp) throws Throwable {
        MethodSignature methodSignature =  (MethodSignature) pjp.getSignature();
        Method method = methodSignature.getMethod();
        
        AdminMsgReload annotation = method.getAnnotation(AdminMsgReload.class);
        AdminMsg msg = AdminMsgClient.getAdminMsg(annotation.clazz());
        if (msg != null) {
            AdminMsgClient.getAdminMsg(annotation.clazz()).reloadMsg();
        }
        
    }
}
